﻿using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.MSSQLDAL
{
    public class UserInfoDAL : UcAsp.Net.IDAL.IUserInfoDAL
    {
        public UserInfo Insert(UserInfo model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                entities.UserInfos.Add(model);
                entities.SaveChanges();
                return model;
            }
        }

        public bool Update(UserInfo model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                try
                {
                    System.Data.Entity.Infrastructure.DbEntityEntry<UserInfo> entry = entities.Entry<UserInfo>(model);
                    entry.State = System.Data.EntityState.Modified;
                    entities.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Delete(UserInfo model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<UserInfo> entry = entities.Entry<UserInfo>(model);
                entry.State = System.Data.EntityState.Deleted;
                entities.SaveChanges();
                return true;
            }
        }

        public UserInfo GetUserInfoByID(int id)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.UserInfos.FirstOrDefault(o => o.ID == id);
            }
        }

        public List<UserInfo> SelectByKeyword(int pagesize, int pageindex, out int pagecount, string keyword)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<UserInfo> list = new List<UserInfo>();
                var query = entities.UserInfos.Where(o => o.UserName.Contains(keyword) || o.Email.Contains(keyword));
                pagecount = list.Count;
                list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                return list;
            }
        }

        public List<UserInfo> Select(int pagesize, int pageindex, out int pagecount)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<UserInfo> list = new List<UserInfo>();
                var query = entities.UserInfos;
                pagecount = (query.Count() / pagesize) + 1;
                list = query.OrderBy(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                return list;
            }
        }

        /// <summary>
        /// 用户排序
        /// </summary>
        /// <param name="pagesize"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagecount"></param>
        /// <param name="order">logintime,recommendid</param>
        /// <returns></returns>
        public List<UserInfo> Select(int pagesize, int pageindex, out int pagecount, string order)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                List<UserInfo> list = new List<UserInfo>();
                var query = entities.UserInfos;
                pagecount = list.Count;
                switch (order.ToLower())
                {
                    case "logintime":
                        list = query.OrderByDescending(o => o.LoginTime).Skip(pagesize * pageindex).Take(pagesize).ToList();
                        break;

                    case "recommendid":
                        list = query.OrderByDescending(o => o.RecommendID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                        break;

                    default:
                        list = query.OrderByDescending(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
                        break;
                }
                return list;
            }
        }

        public UserInfo GetUserInfoByNameAndPassword(string username, string password)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.UserInfos.FirstOrDefault(o => o.UserName == username && o.UserPassword == password);
            }
        }

        public UserInfo GetUserInfoByNameAndOpenID(string username, string openid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.UserInfos.FirstOrDefault(o => o.UserName == username && o.OpenID == openid);
            }
        }
    }
}